﻿using Manager.Database;
using Manager.Model;

namespace Manager.Service
{
    public class PhoneService
    {

        private readonly PCCSContext _context;

        public PhoneService(PCCSContext pCCSContext)
        {
            _context = pCCSContext;
        }


        public void AddPhone(Phone phone)
        {
            _context.phones.Add(phone);
            _context.SaveChanges();
        }

        public void DeletePhone(int id)
        {
            var phone = _context.phones.Find(id);
            if (phone != null)
            {
                _context.phones.Remove(phone);
                _context.SaveChanges();
            }
        }

        public void UpdatePhone(Phone phone)
        {
            _context.phones.Update(phone);
            _context.SaveChanges();
        }

        public Phone GetPhone(int id)
        {
            return _context.phones.Find(id);
        }


        public List<Phone> GetPhoneByName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return _context.phones.ToList();
            }
            return _context.phones.Where(p => p.Name.Contains(name)).ToList();
        }



    }
}
